package org.dg.iag.server;

import java.util.ArrayList;

import org.dg.iag.dao.UserDAO;
import org.dg.iag.entity.User;
import org.dg.iag.entity.Users;
import org.dg.iag.interf.entity.UserResource;
import org.dg.iag.util.LoginUtil;
import org.restlet.resource.ServerResource;

public class UserServerResource extends ServerResource implements UserResource{

	@Override
	public User getUser() {
		if ( ! LoginUtil.checkLogin(getRequest()) ) 
			return null;
		String username	= (String)this.getRequestAttributes().get("username");
		User u			= new User(username, username+"md5");
		return u;
	}

	@Override
	public void addUser(User u) {
		if ( ! LoginUtil.checkLogin(getRequest()) ) 
			return;
		UserDAO	udao	= new UserDAO();
		udao.addUser(u);
	}

	@Override
	public Object modifyUser(User u) {
		if ( ! LoginUtil.checkLogin(getRequest()) ) 
			return null;
		
		UserDAO	udao	= new UserDAO();
		udao.modifyUser(u);
		
		Users users	= new Users();
		users.setUsers(new ArrayList<User>());
		users.getUsers().add(u);
		
		return users;
	}

	@Override
	public User getElement() {
		return this.getUser();
	}

}
